/**
 * *********************** ******* ***********************************
 *
 *  
 * 
 *  
 * *******************************************************************
*/

package com.arges.web.domain.tableutil;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.JoinType;

/**
 * 别名表，用于分页表跨表查询
 *
 *  
 * @version   Ver 3.0
 * @since     Ver 3.0
 * @Date      2012-11-1
 *
 */
public class JoinedTable {
    
    private String tableAlias;   //table的别名，对应到pojo里面 one to many, many to one ...的属性名
    private JoinType joinType;   //inner join 还是 left join ...
    
    /**
     * 创建一个别名表，用于分页表跨表查询，join类型为 inner join
     * @param tableAlias table的别名，对应到pojo里面 one to many, many to one ...的属性名
     */
    public JoinedTable(String tableAlias) {
        this.tableAlias = tableAlias;
        this.joinType = JoinType.INNER;
    }
    
    /**
     * 创建一个别名表，用于分页表跨表查询
     * @param tableAlias table的别名，对应到pojo里面 one to many, many to one ...的属性名
     * @param joinType 表的join类型
     */
    public JoinedTable(String tableAlias, JoinType joinType) {
        this.tableAlias = tableAlias;
        this.joinType = joinType;
    }
    
    public String getAliasName() {
        return tableAlias;
    }

    public int getJoinType() {
        return joinType.ordinal();
    }
    
    public static List<JoinedTable> asList(String... tableAliases) {
        List<JoinedTable> joinedTables = new ArrayList<JoinedTable>();

        for (String tableAlias : tableAliases) {
            joinedTables.add(new JoinedTable(tableAlias));
        }
        
        return joinedTables;
    }
    
    public String toString() {
        return "alias: " + tableAlias + ", joinType: " + joinType.name();
    }

}

